<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>作用域 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../03day/section.2.html" />
    
    
    <link rel="prev" href="../03day/section.1.1.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.3"
        data-chapter-title="作用域"
        data-filepath="03day/section.0.3.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4F5C;&#x7528;&#x57DF;">&#x4F5C;&#x7528;&#x57DF;</h1>
<h3 id="&#x4EC0;&#x4E48;&#x662F;&#x547D;&#x540D;&#x7A7A;&#x95F4;">&#x4EC0;&#x4E48;&#x662F;&#x547D;&#x540D;&#x7A7A;&#x95F4;</h3>
<blockquote>
<p>&#x6BD4;&#x5982;&#x6709;&#x4E00;&#x4E2A;&#x5B66;&#x6821;&#xFF0C;&#x6709;10&#x4E2A;&#x73ED;&#x7EA7;&#xFF0C;&#x5728;7&#x73ED;&#x548C;8&#x73ED;&#x4E2D;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x53EB;&#x201C;&#x5C0F;&#x738B;&#x201D;&#x7684;&#x540C;&#x5B66;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x5B66;&#x6821;&#x7684;&#x5E7F;&#x64AD;&#x4E2D;&#x547C;&#x53EB;&#x201C;&#x5C0F;&#x738B;&#x201D;&#x65F6;&#xFF0C;7&#x73ED;&#x548C;8&#x73ED;&#x4E2D;&#x7684;&#x8FD9;2&#x4E2A;&#x4EBA;&#x5C31;&#x7EB3;&#x95F7;&#x4E86;&#xFF0C;&#x4F60;&#x662F;&#x558A;&#x8C01;&#x5462;&#xFF01;&#xFF01;&#xFF01;&#x5982;&#x679C;&#x662F;&#x201C;7&#x73ED;&#x7684;&#x5C0F;&#x738B;&#x201D;&#x7684;&#x8BDD;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x5F88;&#x660E;&#x786E;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x65F6;&#x7684;7&#x73ED;&#x5C31;&#x662F;&#x5C0F;&#x738B;&#x6240;&#x5728;&#x7684;&#x8303;&#x56F4;&#xFF0C;&#x5373;&#x547D;&#x540D;&#x7A7A;&#x95F4;</p>
</blockquote>
<h3 id="globals&#x3001;locals">globals&#x3001;locals</h3>
<blockquote>
<p>&#x5728;&#x4E4B;&#x524D;&#x5B66;&#x4E60;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x65F6;&#xFF0C;&#x7ECF;&#x5E38;&#x4F1A;&#x63D0;&#x5230;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x4E4B;&#x6240;&#x6709;&#x79F0;&#x4E4B;&#x4E3A;&#x5C40;&#x90E8;&#x3001;&#x5168;&#x5C40;&#xFF0C;&#x5C31;&#x662F;&#x56E0;&#x4E3A;&#x4ED6;&#x4EEC;&#x7684;&#x81EA;&#x4F5C;&#x7528;&#x7684;&#x533A;&#x57DF;&#x4E0D;&#x540C;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x4F5C;&#x7528;&#x57DF;</p>
</blockquote>
<ul>
<li><p>locals</p>
<p>  <img src="../Images/Snip20161106_14.png" alt=""></p>
</li>
<li><p>globals</p>
<p>  <img src="../Images/Snip20161106_15.png" alt=""></p>
</li>
</ul>
<h2 id="legb-&#x89C4;&#x5219;">LEGB &#x89C4;&#x5219;</h2>
<p>Python &#x4F7F;&#x7528; LEGB &#x7684;&#x987A;&#x5E8F;&#x6765;&#x67E5;&#x627E;&#x4E00;&#x4E2A;&#x7B26;&#x53F7;&#x5BF9;&#x5E94;&#x7684;&#x5BF9;&#x8C61;</p>
<pre><code>locals -&gt; enclosing function -&gt; globals -&gt; builtins
</code></pre><ul>
<li>locals&#xFF0C;&#x5F53;&#x524D;&#x6240;&#x5728;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF08;&#x5982;&#x51FD;&#x6570;&#x3001;&#x6A21;&#x5757;&#xFF09;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E5F;&#x5C5E;&#x4E8E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5185;&#x7684;&#x53D8;&#x91CF;</li>
<li>enclosing&#xFF0C;&#x5916;&#x90E8;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF08;&#x95ED;&#x5305;&#x4E2D;&#x5E38;&#x89C1;&#xFF09;<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun1</span><span class="hljs-params">()</span>:</span>
  a = <span class="hljs-number">10</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun2</span><span class="hljs-params">()</span>:</span>
      <span class="hljs-comment"># a &#x4F4D;&#x4E8E;&#x5916;&#x90E8;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;</span>
      print(a)
</code></pre>
</li>
<li>globals&#xFF0C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x6240;&#x5728;&#x6A21;&#x5757;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;<pre><code class="lang-python">a = <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">()</span>:</span>
  <span class="hljs-comment"># &#x9700;&#x8981;&#x901A;&#x8FC7; global &#x6307;&#x4EE4;&#x6765;&#x58F0;&#x660E;&#x5168;&#x5C40;&#x53D8;&#x91CF;</span>
  <span class="hljs-keyword">global</span> a
  <span class="hljs-comment"># &#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; local &#x53D8;&#x91CF;</span>
  a = <span class="hljs-number">2</span>
</code></pre>
</li>
<li><p>builtins&#xFF0C;&#x5185;&#x5EFA;&#x6A21;&#x5757;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x3002;</p>
<pre><code>  Python &#x5728;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x81EA;&#x52A8;&#x4E3A;&#x6211;&#x4EEC;&#x8F7D;&#x5165;&#x5F88;&#x591A;&#x5185;&#x5EFA;&#x7684;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#xFF0C;
  &#x6BD4;&#x5982; dict&#xFF0C;list&#xFF0C;type&#xFF0C;print&#xFF0C;&#x8FD9;&#x4E9B;&#x90FD;&#x4F4D;&#x4E8E; __builtin__ &#x6A21;&#x5757;&#x4E2D;&#xFF0C;
  &#x53EF;&#x4EE5;&#x4F7F;&#x7528; dir(__builtin__) &#x6765;&#x67E5;&#x770B;&#x3002;
  &#x8FD9;&#x4E5F;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x6211;&#x4EEC;&#x5728;&#x6CA1;&#x6709; import&#x4EFB;&#x4F55;&#x6A21;&#x5757;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;
  &#x5C31;&#x80FD;&#x4F7F;&#x7528;&#x8FD9;&#x4E48;&#x591A;&#x4E30;&#x5BCC;&#x7684;&#x51FD;&#x6570;&#x548C;&#x529F;&#x80FD;&#x4E86;&#x3002;

  &#x5728;Python&#x4E2D;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x5185;&#x5EFA;&#x6A21;&#x5757;&#xFF0C;&#x8BE5;&#x6A21;&#x5757;&#x4E2D;&#x6709;&#x4E00;&#x4E9B;&#x5E38;&#x7528;&#x51FD;&#x6570;;&#x5728;Python&#x542F;&#x52A8;&#x540E;&#xFF0C;
  &#x4E14;&#x6CA1;&#x6709;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x5458;&#x6240;&#x5199;&#x7684;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x524D;&#xFF0C;Python&#x4F1A;&#x9996;&#x5148;&#x52A0;&#x8F7D;&#x8BE5;&#x5185;&#x5EFA;&#x51FD;&#x6570;&#x5230;&#x5185;&#x5B58;&#x3002;
  &#x53E6;&#x5916;&#xFF0C;&#x8BE5;&#x5185;&#x5EFA;&#x6A21;&#x5757;&#x4E2D;&#x7684;&#x529F;&#x80FD;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#xFF0C;&#x4E0D;&#x7528;&#x5728;&#x5176;&#x524D;&#x6DFB;&#x52A0;&#x5185;&#x5EFA;&#x6A21;&#x5757;&#x524D;&#x7F00;&#xFF0C;
  &#x5176;&#x539F;&#x56E0;&#x662F;&#x5BF9;&#x51FD;&#x6570;&#x3001;&#x53D8;&#x91CF;&#x3001;&#x7C7B;&#x7B49;&#x6807;&#x8BC6;&#x7B26;&#x7684;&#x67E5;&#x627E;&#x662F;&#x6309;LEGB&#x6CD5;&#x5219;&#xFF0C;&#x5176;&#x4E2D;B&#x5373;&#x4EE3;&#x8868;&#x5185;&#x5EFA;&#x6A21;&#x5757;
  &#x6BD4;&#x5982;&#xFF1A;&#x5185;&#x5EFA;&#x6A21;&#x5757;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;abs()&#x51FD;&#x6570;&#xFF0C;&#x5176;&#x529F;&#x80FD;&#x6C42;&#x7EDD;&#x5BF9;&#x503C;&#xFF0C;&#x5982;abs(-20)&#x5C06;&#x8FD4;&#x56DE;20&#x3002;
</code></pre></li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../03day/section.1.1.html" class="navigation navigation-prev " aria-label="Previous page: 循环导入"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../03day/section.2.html" class="navigation navigation-next " aria-label="Next page: ==、is"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
